//app.js
var appInstance = getApp()
App({
  globalData: {
    userInfo: null,
    time: "",
    username: "",
    winWidth: 0,
    winHeight: 0,
    sessionId: "",
    lat: 0,
    long: 0,
    sharekey: '',
    fatherid: '',
    // data_url: "https://192.168.1.137"
    // data_url: "https://192.168.1.107"
    data_url: "https://www.ynny168.com"
  },
  onLaunch: function () {
    this.getSessionId()
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
      //获取系统信息
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            winWidth: res.windowWidth,
            winHeight: res.windowHeight
          })
        },
      })
    }
  },
  setting: function () {
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userLocation']) {
          return true
        } else {
          if (JSON.stringify(res.authSetting) != '{}'){
            wx.showToast({
              title: '请先授权信息',
            })
            setTimeout(function(){
              wx.openSetting({
                success: function (res) {
                  if (res.authSetting['scope.userLocation']) {
                    return true
                  } else {
                    wx.navigateBack({
                      delta: 1
                    })
                  }
                }
              })
            },1500)
          }
        }
      }
    })
  },
  //获取sessionId
  getSessionId: function () {
    var that = this
    if (this.globalData.sessionId == '') {
      return new Promise(function (resolve) {
        wx.login({
          success: function (res) {
            resolve(res)
          }
        })
      }).then(function (res) {
        return new Promise(function (resolve) {
          wx.request({
            url: that.globalData.data_url + "/GPD/wxlogin.do",
            data: {
              js_code: res.code
            },
            success: function (doc) {
              that.globalData.sessionId = doc.data.session_id
              resolve(doc.data.session_id)
              wx.getLocation({
                success: function (loc) {
                  wx.getUserInfo({
                    success: function (res) {
                      wx.request({
                        url: that.globalData.data_url + "/GPD/WXUserInfo.do",
                        data: {
                          iv: res.iv,
                          encryptedData: res.encryptedData,
                          session_id: doc.data.session_id,
                          longitude: loc.longitude,
                          latitude: loc.latitude
                        },
                        success: function (res) {
                          that.globalData.sharekey = res.data.sharekey
                          resolve(doc.data.session_id)
                        }
                      })
                    }
                  })
                }
              })
            }
          })
        })
      })
    } else {
      return new Promise(function (resolve) {
        resolve(that.globalData.sessionId)
      })
    }
  }
})